package com.doule.farmerassistant.autoupdate;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
 * Created by Happy on 2015/1/27.
 */
public class UpdateInfo {
    private final int mVersionCode;
    private final String mUpdateMessage;
    private final String mUrl;

    public UpdateInfo(int vc, String updateMessage, String url) {
        mVersionCode = vc;
        mUpdateMessage = updateMessage;
        mUrl = url;
    }

    private boolean mIsDownloaded;
    private File mApkFile;

    public void done() {
        removeApkFile();
    }

    private void removeApkFile() {
        if (mApkFile != null) {
            mApkFile.delete();
        }
    }

    public void onDownloadedApk(File apkFile) {
        mIsDownloaded = true;
        mApkFile = apkFile;
    }

    public boolean isApkDownloaded() {
        return mIsDownloaded;
    }

    public String getUrl() {
        return mUrl;
    }

    public String getUpdateMessage() {
        return mUpdateMessage;
    }

    public int getVersionCode() {
        return mVersionCode;
    }

    public static UpdateInfo fromJSON(JSONObject jsonObject) throws JSONException {
        int vc = jsonObject.getInt("versionCode");
        String updateMessage = jsonObject.getString("updateMessage");
        String url = jsonObject.getString("url");

        return new UpdateInfo(vc, updateMessage, url);
    }
}
